package hu.myai.triggers.bullish;

import hu.myai.api.tick.AbstractTickProvider;
import hu.myai.api.triggers.AbstractTrigger;
import hu.myai.indicators.trendline.ResistanceLevelIndicator;
import hu.myai.model.TrendLineVO;
import hu.myai.model.TriggerVO;
import hu.myai.util.pool.TrendLinePool;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * if the resistance cross the price curve it gives a sign.
 * */
public class ResistanceCrossTrigger extends AbstractTrigger {

	/**
	 * The object used when logging debug,errors,warnings and info.
	 * */
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	public ResistanceCrossTrigger(String name, AbstractTickProvider tick) {
		super(name, tick);
	}

	public void setResistanceLevelIndicatorParameter(Double parameter) {
		register(new ResistanceLevelIndicator(parameter));
	}

	@Override
	protected TriggerVO triggerCalculation() {

		List<TrendLineVO> resistanceList = TrendLinePool.getResistanceInstance().getLineList(getTick().getName());

		// apply the remove
		for (TrendLineVO line : resistanceList) {
			double yNow = line.getY(lastTick.getTimestamp());
			double distance = lastTick.getTimestamp() - line.getInitDate();

			// tick is above the resistance
			if (yNow < lastTick.getHigh() && line.getTouch() == 0 && distance > 20 /*&& line.getTangens() > 1.25E-6*/ )
				if (tickHistory.get(1).getLow() < tickHistory.get(0).getLow()) {
					TriggerVO trigger = new TriggerVO();
					trigger.setTick(lastTick);
					trigger.setTradeType(TriggerVO.SELL_ONMARKET);

					log.debug(trigger.toString());
					return trigger;
			}
		}
		return null;
	}
}
